<?php
namespace Seller\Model;
use Think\Model\RelationModel;
class GoodModel extends RelationModel
{
    protected $_link = array(

        //关联分类表
        'category'=>array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Category',
            'foreign_key'   => 'cate',
            'mapping_fields' => 'c_name',
            'as_fields' => 'c_name',
        ),

        //关联品牌表
        'brand'=>array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Brand',
            'foreign_key'   => 'brand_id',
            'mapping_fields' => 'b_name,agent_brand',
            'as_fields' => 'b_name,agent_brand',
        )


    );

    //订单里面每一项商品的价格 type=1单件 type=2整箱
    public function goodPrice($gid, $num, $type) {
        $uid = session('user_id');
        $is_agent = M('agent')->where( array('agent_id'=>$uid) )->getField('is_agent');

        if ($is_agent == 1) {
            $good = M('good')->field('id,brand_id')->where( array('id'=>$gid) )->find();
            $res = single_good_amt($good);
            $sum = ($type == 1) ? ($res['unit_amt'] * $num) : ($res['batch_amt'] * $num);
        } else {
            $price = M('good')->where( array('id'=>$gid) )->getField('price');
            $sum = $price * $num;
        }
        return $sum;
    }







}

